package com.chen.apipassenger.service;


import com.chen.apipassenger.remote.ServicePassengerUserClient;
import com.chen.apipassenger.remote.ServiceVerificationCodeClient;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.constant.IdentityConstants;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.request.LoginOrRegisterRequest;
import com.chen.internalcommon.response.NumberCodeResponse;
import com.chen.internalcommon.response.TokenResponse;
import com.chen.internalcommon.util.JwtUtils;
import com.chen.internalcommon.util.RedisPrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class VerificationCodeService {

    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public ResponseResult generateCode(String passengerPhone) {
        log.info("generateCode start");
        log.info("接收到的乘客电话号码：{}", passengerPhone);

        // 获取验证码
        log.info("调用验证码服务获取验证码");
        ResponseResult<NumberCodeResponse> numberCodeResponse = serviceVerificationCodeClient.getNumberCode(6);
        int numberCode = numberCodeResponse.getData().getNumberCode();
        System.out.println("remote code:" + numberCode);
        // 将验证码存入redis
        log.info("将验证码存入redis");
        String key = RedisPrefixUtils.generateCodeKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key, numberCode + "", 2, TimeUnit.MINUTES);
        // TODO 调用短信服务，将验证码发送给乘客的手机
        return ResponseResult.success("");
    }

    public ResponseResult checkCode(String verificationCode, String passengerPhone) {
        log.info("checkCode start");
        // 从redis中获取验证码
        System.out.println("从redis中获取验证码");
        String key = RedisPrefixUtils.generateCodeKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        System.out.println("redis中的code：" + codeRedis);

        // 校验验证码是否正确
        System.out.println("校验验证码是否正确");
        if (codeRedis == null || !codeRedis.equals(verificationCode)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR);
        }
        // 根据手机号判断该用户是否存在并做相应的处理
        servicePassengerUserClient.loginOrRegister(new LoginOrRegisterRequest(passengerPhone));
        // 返回令牌
        String accessToken = JwtUtils.generateAccessToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        String refreshToken = JwtUtils.generateRefreshToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        // token 存储到redis中
        String accessTokenKey = RedisPrefixUtils.generateAccessTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        String refreshTokenKey = RedisPrefixUtils.generateRefreshTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        // 注意 refreshToken的过期时间一定是要比accessToken的过期时间要长的，否则这个双token就没有意义
        stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken, 30, TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken, 31, TimeUnit.DAYS);
        TokenResponse tokenResponse = new TokenResponse(accessToken, refreshToken);
        return ResponseResult.success(tokenResponse);


    }


}
